Flutter / GetX Basics / snackbar using getX
Snackbar using getX
-
Steps
Step 1:Add get package to pubspec.yaml file:
get: Step 2: Import get package in main.dart file
import 'package:get/get.dart'; Step 3: code
For creating an app, use GetMaterialApp instead of MaterialApp because we are using GetX library.
import 'package:flutter/material.dart'; import 'package:get/get.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return GetMaterialApp( title: 'Scaffold demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), debugShowCheckedModeBanner: false, ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('GeeksforGeeks Scaffold'), centerTitle: true, backgroundColor: Colors.green, ), body: Center( child: ElevatedButton( child: Text('Open Snackbar'), onPressed: (){ }, ), ), ); } } create Snackbar using Get.snackbar(title, message);
Complete codeGet.snackbar( "GeeksforGeeks", "Hello everyone", icon: Icon(Icons.person, color: Colors.white), snackPosition: SnackPosition.BOTTOM, );
Some additional parametersimport 'package:flutter/material.dart'; import 'package:get/get.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return GetMaterialApp( title: 'Scaffold demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), debugShowCheckedModeBanner: false, ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('GeeksforGeeks Scaffold'), centerTitle: true, backgroundColor: Colors.green, ), body: Center( child: ElevatedButton( child: Text('Open Snackbar'), onPressed: (){ Get.snackbar( "GeeksforGeeks", "Hello everyone", icon: Icon(Icons.person, color: Colors.white), snackPosition: SnackPosition.BOTTOM, ); }, ), ), ); } } Get.snackbar( "GeeksforGeeks", "Hello everyone", icon: Icon(Icons.person, color: Colors.white), snackPosition: SnackPosition.BOTTOM, backgroundColor: Colors.green, borderRadius: 20, margin: EdgeInsets.all(15), colorText: Colors.white, duration: Duration(seconds: 4), isDismissible: true, dismissDirection: SnackDismissDirection.HORIZONTAL, forwardAnimationCurve: Curves.easeOutBack, );